<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/8/24
 * Time: 11:18
 */

namespace app\index\controller;


use Redis\Redis;
use think\Db;

class DownLoad
{
    function downloadProject()
    {
//        $ip = getsIp();
//        $num = Db::connect("db_config2")->field("num")->table("download")->where(array("id" => 1))->find()['num'];
//        Db::connect("db_config2")->table("download_info")->insert(array("ip" => $ip, 'time' => date('Y-m-d H:i:s')));
//        if (is_null($num)) {
//            Db::connect("db_config2")->table("download")->insert(array("num" => 1));
//        } else {
//            $num++;
//            $num = Db::connect("db_config2")->table("download")->where(array("id" => 1))->update(array("num" => $num));
//        }

        $info = $this->getsVersionControl('__UNI__B488D9A');
        header("Location:" . $info['url']);
//        $files = ROOT_PATH.'APK/cooperation.apk';
//        //打开文件
//        $file1 = fopen($files, "r");
//        //输入文件标签
//        Header("Content-type: application/octet-stream");
//        Header("Accept-Ranges: bytes");
//        Header("Accept-Length: " . filesize($files));
//        Header("Content-Disposition: attachment; filename=E联车服联盟");
//        echo fread($file1, filesize($files));
//        fclose($file1);

    }

    /**
     * @return array
     * @context 更新版本
     */
    public function up_version()
    {
        $appid = input("post.appid") ?? '__UNI__B488D9A';
        $version = input("post.version"); //客户端版本号
        $rsp = array("status" => true, "up_status" => false); //默认返回值，不需要升级
        if (isset($appid) && isset($version)) {
//            $info = $this->getsVersionControl($appid);
//            if (!empty($info) and $appid === $info['appid']) { //校验appid
//                if (!in_array($version, $info['version_list'])) { //这里是示例代码，真实业务上，最新版本号及relase notes可以存储在数据库或文件中
//                    $rsp["up_status"] = true;
//                }
//            }
            //    //全部变成小写字母
            $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
            $type = 'other';
            //分别进行判断
            if (strpos($agent, 'iphone') || strpos($agent, 'ipad')) {
                $type = 'ios';
            }

            if (strpos($agent, 'android')) {
                $type = 'android';
            }
            # 查询最新的版本信息
            $info = Db::table('version_control')
                ->where(array('appid' => $appid))
                ->order(array('version' => 'desc'))
                ->find();
            if ($type != 'android') {
                $info['version'] = $info['ios_version'];
            }
            if (!empty($info) and intval(str_replace('.', '', $version)) < intval(str_replace('.', '', $info['version']))) { //校验appid
                $rsp["up_status"] = true;
            }
            $rsp['version'] = $info['version'];
            $rsp["note"] = $info['note']; //release notes
            $rsp["url"] = $info['url']; //应用升级包下载地址
            $rsp["ios_url"] = $info['ios_url']; //应用升级包下载地址
            $rsp['force_update'] = $info['force_update'];
        }
        return $rsp;
    }

    function getsVersionControl($appid = '__UNI__B488D9A')
    {
        $_redis = new Redis();
        $list = $_redis->hGetJson("version", $appid);
//        if (!empty($list)) {
        $list = Db::table("version_control")->where(array('appid' => $appid))->order("id desc")->find();

        # 查询强制更新之后的版本
        $version_list = Db::table("version_control")->field("version")
            ->where(" id >= ifnull((select id from version_control where appid = '" . $appid . "' and force_update=1 order by id desc limit 1,0),0)")
            ->where(array('appid' => $appid))
            ->select();

        $list['version_list'] = array_values(array_column($version_list, 'version'));
        $_redis->hSetJson("version", $appid, $list);
//        }
        return $list;
    }
}
